﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Super.Common;
using Super.Common.Cache;
using Super.Entities;
using Super.Infrastructure;

namespace Super.DomainService
{
    /// <summary>
    /// 销售Items
    /// </summary>
    public class SellItems
    {
        public int StockId { get; set; }
        public string ProductName { get; set; }
        public int Count { get; set; }
        public decimal Price { get; set; }
    }
    public class CacheService
    {


        public static string SettingCacheKey = "Setting_Cache_Key";
        public static string DiyCategoryKey = "Diy_Category_Key";
        public static SmartCache<SettingInfo> SettingCache = new SmartCache<SettingInfo>(1);
        public static SmartCache<List<Stock>> StockCache = new SmartCache<List<Stock>>(10);
        public static SmartCache<string> StockNoCache = new SmartCache<string>(1);
        public static SmartCache<dynamic> OneCache = new SmartCache<dynamic>(100);
        public static SmartCache<List<ProductCategory>> DiyProductCategoryCache=new SmartCache<List<ProductCategory>>(1); 
        public static SmartCache<List<DiyItem>> DiyItemsCache=new SmartCache<List<DiyItem>>(100);
        public static SmartCache<List<SellItems>> SellStockCache=new SmartCache<List<SellItems>>(50);


        public static void SetOneCache(string key, dynamic value)
        {

            dynamic item;
            OneCache.TryGetValue(key, out item, () =>
            {
                var j = value;
                return j;
            });
            if (item != value)
            {
                OneCache.Remove(key);
                SetOneCache(key, value);
            }

        }

        public static dynamic GetOneCache(string key)
        {
            dynamic item;
            OneCache.TryGetValue(key, out item);
            return item;
        }


        /// <summary>
        /// 获取网站配置
        /// </summary>
        /// <returns></returns>
        public static SettingInfo GetSetting()
        {
            SettingInfo item;
            SettingCache.TryGetValue(SettingCacheKey, out item, () =>
            {
                var adp = new DbContextAdapter();
                var _keyvalueService = new BaseService<KeyValue>(adp);
                var settingModel = _keyvalueService.Repository.Find(a => a.Key == "WebSetting").FirstOrDefault();
                if (settingModel == null)
                {
                    return null;
                }
                var obj = Common.JsonHelper.JsonToObject<SettingInfo>(settingModel.Value);
                return obj;
            });
            return item;
        }


        #region Stock
        public static List<Stock> GetStockList(string key)
        {
            List<Stock> item;
            StockCache.TryGetValue(key, out item);
            return item;
        }

        public static void AddStockList(string key, Stock model)
        {
            List<Stock> item;
            StockCache.TryGetValue(key, out item, () =>
            {
                var temp = new List<Stock>();
                return temp;
            });
            item.Add(model);

        }


        public static string GetStockNo()
        {
            string item;
            StockNoCache.TryGetValue("stockNo", out item, () =>
            {
                var i = Number.GetSerialNumber(4).ToString();
                return i;
            });

            return item;
        }
        #endregion

        #region Diy
        
        public static void SetDiyCategoryCache(List<ProductCategory> list )
        {
            List<ProductCategory> item;
            DiyProductCategoryCache.TryGetValue(DiyCategoryKey, out item, () =>
                                                                              {
                                                                                  var j = list;
                                                                                  return j;
                                                                              });
        }

        public static List<ProductCategory> GetDiyCategoryCache()
        {
            List<ProductCategory> item;
            DiyProductCategoryCache.TryGetValue(DiyCategoryKey, out item);
            return item;
        }

        /// <summary>
        /// 获取DiyitemList
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static List<DiyItem> GetDiyItemList(string key)
        {
            List<DiyItem> items;
            DiyItemsCache.TryGetValue(key, out items);
            if(items==null)
            {
                return new List<DiyItem>();
            }
            return items;
        }

        /// <summary>
        /// 设置DiyItems
        /// </summary>
        /// <param name="key"></param>
        /// <param name="items"></param>
        public static void SetDiyItemList(string key,List<DiyItem> items)
        {
            DiyItemsCache.Remove(key);
            List<DiyItem> item;
            DiyItemsCache.TryGetValue(key, out item, () => items);
        }

        #endregion

        #region Sells

        /// <summary>
        /// 设置销售项
        /// </summary>
        /// <param name="key"></param>
        /// <param name="item"></param>
        public static void SetSellItems(string key,List<SellItems> item)
        {
            List<SellItems> items;
            SellStockCache.Remove(key);
            SellStockCache.TryGetValue(key,out items, ()=> item);
        }
        /// <summary>
        /// 获取销售项
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static List<SellItems> GetSellItems(string key)
        {
            List<SellItems> item;
            SellStockCache.TryGetValue(key, out item);
            return item ?? new List<SellItems>();
        }
        #endregion


    }
}
